// #ifdef H5
import NativeShare from 'nativeshare'
const nativeShare = new NativeShare()
const jweixin = require('js_sdk/jweixin-module/index.js')
// #endif
import {
  getdateStatus,
  isMoveStatus,
  setClipboardData,
  getUpdateDate,
  getOrganizationalStatus,
} from '@/common/type'
import { getMoveOperateTimes, addUserMoveOperateTimes, getMoveSearchInfo } from 'api/move'
import {routerWebViewGd} from '@/common/util'
export const share1 = {
  data() {
    return {
      id: '',
      details: {}, //活动详情
      pageviews: 0, //浏览量
      MoveInfo: {},
      tabType: 1, // tab切换 1 个人  2  企业
      isWx: false,
      isShow: false,
      shareInfo: {
        title: '',
        summary: '',
        imageUrl: '',
        href: '',
      },
      news: {
        publishTime: '',
        newsContent: '',
        newsTitle: '',
        newsOrg: '',
        id: '1',
        isPublish: '',
      },
      //分享
      shareList: [
        {
          type: 1,
          icon: '/static/img/share_wechat.png',
          text: '微信好友',
        },
        {
          type: 2,
          icon: '/static/img/share_moment.png',
          text: '朋友圈',
        },
      ],
      newsOperateTimes: {
        newsId: '',
        readTimes: 0,
        shareTimes: 0,
        dzTimes: 0,
      },
      mynewsOperateTimes: {
        newsId: '',
        readTimes: 0,
        shareTimes: 0,
        dzTimes: 0,
      },
      isLive: false, //是否是直播活动类型
      activeTypeValue: 2, //福利活动
    }
  },
  onLoad(options) {
    this.id = options.id
    if (options.values) {
      this.activeTypeValue = +options.values
    }
    this.isLiveValue(options)
    // #ifdef MP || MP-WEIXIN
    uni.showShareMenu({
      withShareTicket: true,
      //设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
      menus: ['shareAppMessage', 'shareTimeline'],
    })
    // #endif
  },
  onShow() {
    // #ifdef H5
    setTimeout(() => {
      const that = this
      if (this.news) {
        // console.log(that.news && that.news.newsTitle, 111)
        // console.log(that.news && that.news.imageUrl, 111)
      }
      if (this.details) {
        // console.log(that.details && this.details.moveTitle, 3111)
        // console.log(that.details && this.details.movePic, 3111)
      }

      this.$wxSetShare({
        title: that.news.newsTitle || that.details.moveTitle, // 分享标题
        desc: that.news.newsTitle || that.details.moveTitle, // 分享描述
        imgUrl: that.shareInfo.imageUrl || that.details.movePic, // 分享图标
      })
    }, 1000)
    // #endif
  },
  computed: {
    //如果报名人数大于总报名人数，按钮隐藏
    isBtnMoveJoinNum() {
      const { personNumber, moveNum } = this.details
      if (moveNum > 0) {
        return !(personNumber >= moveNum)
      }
      return true
    },
    //报名状态
    moveStatus() {
      /**区别组织活动和其他 */
      // console.log(this.details, this.isBtnMoveJoinNum)
      const { activityCategory, dateStatus } = this.details
      if (activityCategory == 1) return getOrganizationalStatus(dateStatus, !this.isBtnMoveJoinNum)
      return getdateStatus(dateStatus)
    },
    //是否可以报名或者预约
    isMoveStatus() {
      /**人数已满>福利兑换中>抢福利中 */
      const { dateStatus } = this.details
      return isMoveStatus(dateStatus, this.activeTypeValue)
    },
  },
  mounted() {
    this.getMoveOperateTimes({
      moveId: this.id,
    })
    this.addUserMoveOperateTimes({
      category: '1',
      moveId: this.id,
    })
    this.getMoveSearchInfo({
      moveId: this.id,
    })
  },
  methods: {
    //判断它是否是直播活动方式
    isLiveValue({ type }) {
      if (type === 'live') {
        this.isLive = true
      }
    },
    //定位操作
    onPosConfigs() {
      const { moveDimension, moveLongitude, moveAddr } = this.details
      // uni.openLocation({
      //   name: moveAddr,
      //   address: moveAddr,
      //   latitude: moveDimension,
      //   longitude: moveLongitude,
      //   success: function () {
      //     console.log('success')
      //   },
      // })
      return routerWebViewGd({log:moveLongitude,lat:moveDimension,name:moveAddr})
    },
    //活动-详情
    getMoveSearchInfo(id) {
      getMoveSearchInfo(id)
        .then((res) => {
          const { code, result } = res
          if (code === 200) {
            this.getMoveRbDesc(result)
          }
        })
        .catch((e) => {})
    },
    //更新富文本相关
    getMoveRbDesc(result) {
      this.details = result
      const { dateendTime, startTime, endTime, moveDesc } = result
      this.details.dateendTime = getUpdateDate(dateendTime)
      this.details.startTime = getUpdateDate(startTime)
      this.details.endTime = getUpdateDate(endTime)
      this.details.moveDesc = moveDesc
        .replace(/\<img/gi, '<img class="box_img"')
        .replace(/\<p/gi, '<p class="box_p"')
        .replace(/\<span/gi, '<span class="box_span"')
        .replace(/&nbsp;/g, '\xa0')
        .replace(/; width: .*?;/g, '')
    },
    //活动信息-活动人数
    gpMovedatePerson(id) {
      getMoveSearchInfo(id)
        .then((res) => {
          console.log(res)
        })
        .catch((e) => {})
    },
    //用户活动操作次数-增加1次-1.阅读2.点赞3.分享
    addUserMoveOperateTimes(data) {
      addUserMoveOperateTimes(data)
        .then((res) => {})
        .catch((e) => {
          goLogin()
        })
    },
    //获取活动总操作次数
    getMoveOperateTimes(moveId) {
      getMoveOperateTimes(moveId)
        .then((res) => {
          const { code, result } = res
          if (code === 200) {
            this.pageviews = result.readTimes
          }
        })
        .catch((e) => {
          console.log(e)
        })
    },
    preview(src, e) {
      // do something
    },
    navigate(href, e) {
      // do something
    },

    onClose(e) {
      this.isShow = false
    },
    //点赞
    onLinks(cb, param) {
      cb(param).then((res) => {
        if (res.success) {
          this.newsOperateTimes.dzTimes += 1
          this.mynewsOperateTimes.dzTimes = 1
          this.$api.msg('点赞成功')
          return
        } else {
          this.$api.msg(res.message)
        }
      })
    },
    //分享
    onShare() {
      //判断是否微信浏览器
      if (this.isWx) {
        // #ifdef H5
        this.isShow = true
        // #endif
        // #ifdef MP-WEIXIN
        this.routerGetCurrentPages()
        // #endif
      } else {
        this.getShareData()
      }
    },
    //进小程序里面
    routerGetCurrentPages() {
      console.log('我在小程序环境里面操作了')
      const routes = getCurrentPages()
      const { route: curRoute, id: idRoute } = routes[routes.length - 1]
      const joinUrlId = `${window.location.href.split('#')[1]}`
      const aabbUrl = `${encodeURIComponent(joinUrlId)}`
      const tempUrls = `https://hzgsapp3.hzgsredcross.org.cn/sharequntuan.aspx?shareurl=${aabbUrl}&title=${
        encodeURIComponent(this.news.newsTitle) || encodeURIComponent(this.details.moveTitle)
      }`
      const url = `/pages/webview/webview?url=${tempUrls}`
      uni.navigateTo({
        url,
      })
    },
    //第三方分享
    getShareData() {
      const self = this
      let tag = uni.getStorageSync('tag')
      let link = ''
      if(!tag){
        link=window.location.href
      }else {
        link=`${window.location.href}&tag=${tag}`
      }
      const shareData = {
        title:
          (self.news && self.news.newsTitle) ||
          (self.shareInfo && self.shareInfo.title) ||
          (self.details && self.details.moveTitle),
        desc:
          (self.news && self.news.newsTitle) ||
          (self.shareInfo && self.shareInfo.title) ||
          (self.details && self.details.moveTitle),
        // 如果是微信该link的域名必须要在微信后台配置的安全域名之内的。
        link,
        icon: self.details && self.details.movePic,
        // 不要过于依赖以下两个回调，很多浏览器是不支持的
        success() {
          self.$api.msg('分享成功')
        },
        fail() {
          self.$api.msg('分享失败')
        },
        syncDescToTag: true,
        syncIconToTag: true,
        syncTitleToTag: true,
      }
      nativeShare.setShareData(shareData)
      try {
        nativeShare.call()
      } catch (err) {
        // 如果不支持，你可以在这里做降级处理
        uni.setClipboardData({
          data: window.location.href,
          success: (data) => {
            this.$api.msg('已复制到剪切板,请到微信,钉钉等平台粘贴分享')
          },
          fail: (error) => {
            console.log(error, 'fail')
          },
          complete: (res) => {
            console.log(res, 'complete')
          },
        })
      }
    },
    changeTabType(type) {
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 30,
      })
      this.tabType = type
    },
  },

  // 这里如果写成onShareAppMessage: res => { ... }形式的箭头函数，在内部会无法获得this
  onShareAppMessage(res) {
    console.log(res, 'onShareAppMessage')
    return {
      title:
        (this.news && this.news.newsTitle) ||
        (this.shareInfo && this.shareInfo.title) ||
        (this.details && this.details.moveTitle),
      path: this.id
        ? `/pages/activityDetail/activityDetail?id=${this.id}`
        : `/pages/newsDetail/newsDetail?id=${this.news.id}`,
      imageUrl: this.shareInfo.movePic || this.details.movePic,
    }
  },
  //这个只安卓端的分享到朋友圈
  onShareTimeline(res) {
    console.log(res, 'onShareTimeline')
    return {
      title:
        (this.news && this.news.newsTitle) ||
        (this.shareInfo && this.shareInfo.title) ||
        (this.details && this.details.moveTitle),
      query: this.id
        ? `/pages/activityDetail/activityDetail?id=${this.id}`
        : `/pages/newsDetail/newsDetail?id=${this.news.id}`,
      imageUrl: this.shareInfo.movePic || this.details.movePic,
    }
  },
}
